*******************************************************************************
* Purpose: Study the timing of the impacts on maternal mental health care. 
* Instead of accumulated uptake now quarterly uptake
******************************************************************************
do "D:\Data\workdata\708296\Project - Mother groups and mental health\Do\global.do" 

use "$work\final_data1", clear

foreach v in cons psyk { // quarterly aggregation
gen `v'_q1 = 0 
replace `v'_q1 = max(gpmw_`v'1,gpmw_`v'2,gpmw_`v'3)
gen `v'_q2 = 0 
replace `v'_q2 = max(gpmw_`v'4,gpmw_`v'5,gpmw_`v'6)
gen `v'_q3 = 0 
replace `v'_q3 = max(gpmw_`v'7,gpmw_`v'8,gpmw_`v'9)
gen `v'_q4 = 0 
replace `v'_q4 = max(gpmw_`v'10,gpmw_`v'11,gpmw_`v'12)
gen `v'_q5 = 0 
replace `v'_q5 = max(gpmw_`v'13,gpmw_`v'14,gpmw_`v'15)
gen `v'_q6 = 0 
replace `v'_q6 = max(gpmw_`v'16,gpmw_`v'17,gpmw_`v'18)
gen `v'_q7 = 0 
replace `v'_q7 = max(gpmw_`v'19,gpmw_`v'20,gpmw_`v'21)
gen `v'_q8 = 0 
replace `v'_q8 = max(gpmw_`v'22,gpmw_`v'23,gpmw_`v'24)
}
foreach v in out inp {
gen `v'_q1 = 0 
replace `v'_q1 = max(`v'1,`v'2,`v'3)
gen `v'_q2 = 0 
replace `v'_q2 = max(`v'4,`v'5,`v'6)
gen `v'_q3 = 0 
replace `v'_q3 = max(`v'7,`v'8,`v'9)
gen `v'_q4 = 0 
replace `v'_q4 = max(`v'10,`v'11,`v'12)
gen `v'_q5 = 0 
replace `v'_q5 = max(`v'13,`v'14,`v'15)
gen `v'_q6 = 0 
replace `v'_q6 = max(`v'16,`v'17,`v'18)
gen `v'_q7 = 0 
replace `v'_q7 = max(`v'19,`v'20,`v'21)
gen `v'_q8 = 0 
replace `v'_q8 = max(`v'22,`v'23,`v'24)
}
forvalues i =1/8 {
gen adm_q`i' = max(out_q`i',inp_q`i')	
}
eststo clear
forvalue i = 1/8 {
eststo: areg psyk_q`i' peer_treat, absorb(time fe mfr_first) vce(cluster group) 
parmest, format(estimate min95 max95 %8.3f p %8.1e) saving("$work\timing_psyk`i'", replace)
}
eststo clear
forvalue i = 1/8 {
eststo: areg cons_q`i' peer_treat, absorb(time fe mfr_first) vce(cluster group) 
parmest, format(estimate min95 max95 %8.3f p %8.1e) saving("$work\timing_cons`i'", replace)
}
forvalue i = 1/8 {
eststo: areg adm_q`i' peer_treat, absorb(time fe mfr_first) vce(cluster group) 
parmest, format(estimate min95 max95 %8.3f p %8.1e) saving("$work\timing_adm`i'", replace)
}

********************************************************************************
* Figure C7: Timing of effects of exposure to a depressed peer on maternal mental health care usage
********************************************************************************

use "$work\timing_cons1",clear
gen m = 1
forvalue v = 2/8 {
append using "$work\timing_cons`v'"
replace m = `v' if m == .
}
keep if parm == "peer_treat"

replace t = round(t,0.001)
replace stderr = round(stderr,0.001)
twoway (scatter estimate m, msize(small) msymbol(Oh) mcolor(red) alwidth(none) lcolor(black) lwidth(thick) mlabel(estimate) mlabp(12) mlabs(small) mlabg(15pt) mlabc(black)) /// 
(scatter estimate m, msize(small) msymbol(Oh) mcolor(red) alwidth(none) lcolor(black) lwidth(thick) mlabel(stderr) mlabp(12) mlabs(small) mlabg(6pt) mlabc(black)) ///
(line estimate m, fcolor(black%10) alwidth(none) lcolor(red) lwidth(thick)) ///
(line max95 m, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(medium) lpattern(dash)) ///
(line min95 m, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(medium) lpattern(dash)), $gsq legend(off) xtitle("Quarters after birth") ytitle("Difference") ylab(-0.01(0.01)0.015, tlcolor(gs10) labgap(6-pt) format(%4.3f)) yline(0, lcolor(gs10) lwidth(medthick))
graph export "$out\timing_cons.png", replace


use "$work\timing_psyk1",clear
gen m = 1
forvalue v = 2/8 {
append using "$work\timing_psyk`v'"
replace m = `v' if m == .
}
keep if parm == "peer_treat"

replace t = round(t,0.001)
replace stderr = round(stderr,0.001)
twoway (scatter estimate m, msize(small) msymbol(Oh) mcolor(red) alwidth(none) lcolor(black) lwidth(thick) mlabel(estimate) mlabp(12) mlabs(small) mlabg(15pt) mlabc(black)) /// 
(scatter estimate m, msize(small) msymbol(Oh) mcolor(red) alwidth(none) lcolor(black) lwidth(thick) mlabel(stderr) mlabp(12) mlabs(small) mlabg(6pt) mlabc(black)) ///
(line estimate m, fcolor(black%10) alwidth(none) lcolor(red) lwidth(thick)) ///
(line max95 m, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(medium) lpattern(dash)) ///
(line min95 m, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(medium) lpattern(dash)), $gsq legend(off) xtitle("Quarters after birth") ytitle("Difference") ylab(-0.01(0.01)0.02, tlcolor(gs10) labgap(6-pt) format(%4.2f)) yline(0, lcolor(gs10) lwidth(medthick))
graph export "$out\timing_psyk.png", replace

use "$work\timing_adm1",clear
gen m = 1
forvalue v = 2/8 {
append using "$work\timing_adm`v'"
replace m = `v' if m == .
}
keep if parm == "peer_treat"

replace t = round(t,0.001)
replace stderr = round(stderr,0.001)
twoway (scatter estimate m, msize(small) msymbol(Oh) mcolor(red) alwidth(none) lcolor(black) lwidth(thick) mlabel(estimate) mlabp(12) mlabs(small) mlabg(15pt) mlabc(black)) /// 
(scatter estimate m, msize(small) msymbol(Oh) mcolor(red) alwidth(none) lcolor(black) lwidth(thick) mlabel(stderr) mlabp(12) mlabs(small) mlabg(6pt) mlabc(black)) ///
(line estimate m, fcolor(black%10) alwidth(none) lcolor(red) lwidth(thick)) ///
(line max95 m, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(medium) lpattern(dash)) ///
(line min95 m, fcolor(black%30) alwidth(none) lcolor(gs10) lwidth(medium) lpattern(dash)), $gsq legend(off) xtitle("Quarters after birth") ytitle("Difference") ylab(-0.005(0.005)0.01, tlcolor(gs10) labgap(6-pt) format(%4.3f)) yline(0, lcolor(gs10) lwidth(medthick))
graph export "$out\timing_adm.png", replace